package cn.wolfcode.trip.app.web.controller;

import cn.wolfcode.trip.base.domain.*;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.query.StrategyQueryObject;
import cn.wolfcode.trip.base.service.IStrategyCatalogService;
import cn.wolfcode.trip.base.service.IStrategyDetailService;
import cn.wolfcode.trip.base.service.IStrategyService;
import cn.wolfcode.trip.base.service.ITravelService;
import cn.wolfcode.trip.base.util.JSONResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/strategies")
public class StrategyController {
    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private IStrategyCatalogService catalogService;
    @Autowired
    private IStrategyDetailService detailService;
    @Autowired
    private ITravelService travelService;

    @GetMapping("/{id}")
    @ResponseBody
    public Object getStrategy(@PathVariable Long id) {
        Strategy strategy = strategyService.selectByPrimaryKey(id);
        return strategy;
    }


    //查询所有推荐的攻略
    @GetMapping
    @ResponseBody
    public Object getStrategys(StrategyQueryObject qo) {
        //设置状态是推荐的2

        PageInfo pageInfo = strategyService.getList(qo);
        return  pageInfo;
    }
    //查询攻略中的游记(大家都在看)
    @GetMapping("/{id}/travel")
    @ResponseBody
    public Object getTravelsByStrategyId(@PathVariable Long id){
        Strategy strategy = strategyService.selectByPrimaryKey(id);
        return strategy;
    }
    @GetMapping("/{id}/catalogs")
    @ResponseBody
    public Object getCatalogsByStrategyId(@PathVariable Long id){
        List<StrategyCatalog> catalogs=catalogService.getStrategyCatalogByStrategyId(id);
        return catalogs;
    }


    @GetMapping("/{id}/details")
    @ResponseBody
    public Object getDetailsByStrategyId(@PathVariable Long id){
        List<StrategyDetail> details = detailService.getDetailsBycatalogId(id);
        return details;
    }

    @GetMapping("/details/{id}")
    @ResponseBody
    public Object getDetailsContentById(@PathVariable Long id){
        StrategyDetail detail = detailService.selectByPrimaryKey(id);
        return detail;
    }

    @PostMapping("/{id}/wish")
    @ResponseBody
    public Object saveWish(@PathVariable Long id,Integer state){
        JSONResult result = new JSONResult();
        try{
            strategyService.saveWish(id,state);
        }catch (Exception e){
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }

    @GetMapping("/wish")
    @ResponseBody
    public Object getWish(){
        List<Strategy> list1 =  strategyService.selectWish(1);
        List<Strategy> list2 =  strategyService.selectWish(2);
        Map<String,Object> map = new HashMap<>();
        map.put("list1",list1);
        map.put("list2",list2);

        return map;
    }

    @GetMapping("/search")
    @ResponseBody
    public Object getSearch(QueryObject qo){
        return strategyService.selectByKeyword(qo);
    }

    @GetMapping("/travel")
    @ResponseBody
    public Object getSearchAll(QueryObject qo){
        List<Travel> list = travelService.selectByKeyword(qo);
        List<Strategy> pageInfo = strategyService.selectByKeyword(qo);
        Map<String,Object> map = new HashMap<>();
        map.put("info",pageInfo);
        map.put("list",list);
        return map;
    }

}
